<?php
require($_SERVER['DOCUMENT_ROOT'] . '/setting.php');
require($_SERVER['DOCUMENT_ROOT'] . '/admin/pz/mailimap.php');
function x_real_ip()
{
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] as $xip) {
            if (!preg_match('#^(10|172\\.16|192\\.168)\\.#', $xip)) {
                $ip = $xip;
            } else {
                continue;
            }
        }
    } else {
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
                $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
            } else {
                if ((isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP']))) {
                    $ip = $_SERVER['HTTP_X_REAL_IP'];
                }
            }
        }
    }
    return $ip;
}
function auth_happenone()
{
    global $setting_mail_array, $website_array;
    $datenows = date('YmdHmi');
    $auth_anotheronekey = x_real_ip() . $datenows;
    $domain = $_SERVER['HTTP_HOST'];
    $x_feak = hash_hmac('sha256', $domain . $website_array['admin'] . $setting_mail_array['smtp_user'] . $auth_anotheronekey, $website_array['quascode']);
    return $x_feak;
}
function auth_mailgoto()
{
    $selipose = '';
    global $setting_mail_array, $website_array, $mail_imaps;
    $datenows = date('YmdHmi');
    $auth_anotheronekey = x_real_ip() . $datenows;
    $imapHost = $mail_imaps['imap_host'];
    $imapUser = $setting_mail_array['smtp_user'];
    $imapPassword = $setting_mail_array['smtp_pass'];
    $expectedSubject = 'auth-login';
    $domain = $_SERVER['HTTP_HOST'];
    $expectedContent = hash_hmac('sha256', $domain . $website_array['admin'] . $setting_mail_array['smtp_user'] . $auth_anotheronekey, $website_array['quascode']);
    $timeLimit = 300;
    $currentTime = time();
    $imapStream = imap_open($imapHost, $imapUser, $imapPassword);
    if (!$imapStream) {
        $selipose =  ['status' => 0, 'message' => 'Unable to connect to mail server'];
        return $selipose;
    }
    $startTime = $currentTime - $timeLimit;
    $searchCriteria = 'UNSEEN SINCE ' . date('d-M-Y', $startTime);
    $emails = imap_search($imapStream, $searchCriteria);
    if ($emails) {
        foreach ($emails as $emailNumber) {
            $header = imap_headerinfo($imapStream, $emailNumber);
            $overview = imap_fetch_overview($imapStream, $emailNumber, 0);
            $message = imap_fetchbody($imapStream, $emailNumber, 1);
            $subject = $overview[0]->subject;
            $subject = quoted_printable_decode($subject);
            $subject = mb_decode_mimeheader($subject);
            $structure = imap_fetchstructure($imapStream, $emailNumber);
            $body = $message;
            if ($structure->encoding == 3) {
                $body = base64_decode($body);
            } elseif ($structure->encoding == 4) {
                $body = quoted_printable_decode($body);
            }
            $body = base64_decode($body);
            $mailTime = strtotime($overview[0]->date);
            $fromwho_mail = $header->from[0]->mailbox . "@" . $header->from[0]->host;
            if (strcasecmp($subject, $expectedSubject) === 0 && trim($body) === $expectedContent && $imapUser === $fromwho_mail) {
                $selipose =  ['status' => 1, 'message' => 'Verification successful'];
            } else {
                $selipose =  ['status' => 0, 'message' => 'Validation failed'];
            }
            imap_delete($imapStream, $emailNumber);
        }
        imap_expunge($imapStream);
    } else {
        $selipose =  ['status' => 0, 'message' => 'No eligible messages were found'];
    }
    imap_close($imapStream);
    return $selipose;
}
